<?php
// +----------------------------------------------------------------------
// | 杭州十禾信息科技有限公司
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2015 http://www.shelpe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LeePuman <ysyzlsm@163.com>
// +----------------------------------------------------------------------

class AccountAction extends Action
{
	
	// 登陆的情况自动初始化用户信息
	public function _initialize()
	{
		$this->menuList = $this->getMenu();
		if(strtolower(MODULE_NAME) != 'index') {
			$res = getOptionName($this->menuList, MODULE_NAME, ACTION_NAME);
			$this->moduleInfo = $res['module'];
			$this->actionInfo = $res['action'];
			$this->subMenu = $res['subMenu'];
		}
	}
    
    public function loadJS()
    {
        $js = array();
        $files = func_get_args();
        foreach($files as $f){
        	$js[] =  $f . ".js";
        }
        $this->javascript = array_merge($this->javascript , $js);
    }

	public function display_json($r , $code = RetStatus::OK)
	{
		$dom = $this->_get('dom');
		echo json_encode(array(
			'status' => $code,
			'data' => $r,
			'dom' => $dom
		));
		exit;
	}
	
	public function error( $msg , $code = 500 , $ajax=false)
	{
	    if( $this->isAjax() && $ajax === false){
	        $this->display_json( array('msg' => $msg) , $code );
	    }else{
	        parent::error($msg);
	    }
	}

	public function getMenu($where = array())
	{
		// S('sinbad_menu', null);
		$menu = S('sinbad_menu');
		if(!$menu) {
			$where['status'] = 1;
			$tmp = M('admin_menu')->field('status', true)->where($where)->order('`order` asc')->select();
			$menu = array();
			
			foreach ($tmp as $key => $m) {
				if($m['lavel'] == 0) {
					$menu[$m['id']] = array(
							'info' => array(
									'name' => $m['menu_name'],
									'url' => $m['menu_url'],
									'lavel' => $m['lavel']
								),
							'list' => array()
						);
					unset($tmp[$key]);
				}
			}

			foreach ($tmp as $t) {
				foreach ($menu as $key => $m) {
					if((int)$t['parent_id'] == $key) {
						$menu[$key]['list'][$t['id']] = array(
										'name' => $t['menu_name'],
										'url' => $t['menu_url'],
										'lavel' => $t['lavel']
									);
					}
				}
			}
			S('sinbad_menu', $menu);
		}
		return $menu;
	}
}
